﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Drawing;
using System.Web.Services;
using System.Collections.Generic;

public partial class JOINT_frmJ_TraineeRewPunInput : System.Web.UI.Page
{
    private int? currentId = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("入学情報（賞罰）登録");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("input");
        //check session login
        if (Session["役"] == null || Session["役"].ToString() == "")
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
        if (Session["ID"] == null)
        {
            Response.Redirect("frmJ_TraineeEnterSchoolSearch.aspx");
        }

        if (!IsPostBack)
        {
            ShowGrid();
            this.DataColumnView();
            btn_登録.Visible = true;
            btn_更新.Visible = false;

        }
        Txt_賞罰名.Focus();
        btn_登録完了.Enabled = false;

        validateTxt_賞罰名.ErrorMessage = GetError("ER00001");
        validatetxt_賞罰年月日.ErrorMessage = GetError("ER00001");
        regular1.ErrorMessage = GetError("ER00006");
        validatetxt賞罰特記事項.ErrorMessage = GetError("ER00001");

    }
    /// <summary>
    /// CONVERT DATE
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string ConvertDate(string str)// ham convert ngay thang nam.
    {
        if (str == "")
        {
            return str = "";
        }
        else
        {
            string date = str;
            DateTime d1 = DateTime.Parse(date);
            d1.Year.ToString();
            return date = d1.ToString("yyyy年MM月dd日");
        }
    }
    protected void btn_キャンセル_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_TraineeEnterSchoolSearch.aspx");
    }
    protected void btn_登録完了_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataView dv = new DataView(dt);
            dv.Sort = "IsInit";

            賞罰情報Manager tns = new 賞罰情報Manager();
            if (ViewState["CurrentTable1"] != null)
            {
                foreach (DataRow dataRow1 in dv.Table.Rows)
                {
                    int id = Int32.Parse(dataRow1["IsInit"].ToString());
                    if (id == 0 || id == 1)
                    {
                        string 賞罰名 = dataRow1["賞罰名"].ToString();
                        DateTime 賞罰年月日 = DateTime.Parse(dataRow1["賞罰年月日"].ToString());
                        string 賞罰特記事項 = dataRow1["賞罰特記事項"].ToString();
                        int 社員ID = Int32.Parse(Session["ID"].ToString());
                        賞罰情報 oj = new 賞罰情報();
                        oj.賞罰名 = 賞罰名;
                        oj.賞罰年月日 = 賞罰年月日;
                        oj.賞罰特記事項 = 賞罰特記事項;
                        oj.社員ID = 社員ID;
                        oj.廃止区分 = 0;
                        tns.Insert_frmJ_TraineeRewPunInput(oj);
                    }
                    if (id == 2 && Session["Array"] != null)
                    {
                        List<String> myAL1 = (List<String>)Session["Array"];
                        foreach (string str in myAL1)
                        {

                            int temp = Int32.Parse(dataRow1["ID"].ToString());
                            if (Int32.Parse(str) == temp)
                            {
                                string 賞罰名 = dataRow1["賞罰名"].ToString();
                                DateTime 賞罰年月日 = DateTime.Parse(dataRow1["賞罰年月日"].ToString());
                                string 賞罰特記事項 = dataRow1["賞罰特記事項"].ToString();
                                賞罰情報 ui = new 賞罰情報();
                                ui.賞罰名 = 賞罰名;
                                ui.賞罰年月日 = 賞罰年月日;
                                ui.賞罰特記事項 = 賞罰特記事項;
                                ui.ID = temp;
                                tns.Update_frmJ_TraineeRewPunInput(ui);
                            }
                        }
                    }
                }
                Session.Add("登録完了Status", null);
            }
            this.ShowGrid();
            dt.Rows.Clear();
            btn_登録完了.Enabled = false;
            Session.Remove("Array");
        }
        catch { }
        Response.Redirect("frmJ_TraineeEnterSchoolSearch.aspx");
    }
    /// <summary>
    /// BIND DATE INTO GRIDVIEW
    /// </summary>
    public void ShowGrid()
    {
        int 社員ID = Int32.Parse(Session["ID"].ToString());
        DataTable dt = new DataTable();
        賞罰情報Manager frm = new 賞罰情報Manager();
        dt = frm.ShowGrid賞罰情報(社員ID);
        DataColumn col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "IsInit";
        dt.Columns.Add(col);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["IsInit"] = "3";
        }

        if (dt.Rows.Count > 0)
        {
            grv1.Visible = false;
            grv.Visible = true;
            grv.DataSource = dt;
            ViewState["CurrentTable"] = dt;
            grv.DataBind();
        }
        else
        {
            grv1.Visible = true;
            grv.Visible = false;
            grv.DataSource = dt;
            ViewState["CurrentTable"] = dt;
            AppCode_Common.ShowEmtyGrid<本社履歴>(grv1, 3, "データがありません。", "");

        }

    }
    /// <summary>
    /// ADD NEW ROW FOR GRIDVIEW
    /// </summary>
    protected void AddNewRow()
    {
        try
        {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                grv1.Visible = false;
                grv.Visible = true;
                drCurrentRow = dtCurrentTable.NewRow();

                drCurrentRow["賞罰名"] = Txt_賞罰名.Text.ToString().Trim();
                drCurrentRow["賞罰年月日"] = txt_賞罰年月日.Text.ToString();
                drCurrentRow["賞罰特記事項"] = txt賞罰特記事項.Text.Trim().ToString();
                drCurrentRow["IsInit"] = "1";
                dtCurrentTable.Rows.Add(drCurrentRow);

                DataView dv = new DataView(dtCurrentTable);
                dv.Sort = "IsInit";
                ViewState["CurrentTable"] = dv.ToTable();
                grv.DataSource = dv;
                grv.DataBind();
                Session.Add("登録完了Status", true);
            }
        }
        catch (Exception e) { }
    }
    public void DataCurrentTable()
    {
        try
        {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrrentTable1"];
                grv1.Visible = false;
                grv.Visible = true;
                DataRow drCurrentRow = null;

                drCurrentRow = dt.NewRow();
                drCurrentRow["賞罰名"] = Txt_賞罰名.Text.Trim().ToString();
                drCurrentRow["賞罰年月日"] = txt_賞罰年月日.Text.ToString();
                drCurrentRow["賞罰特記事項"] = txt賞罰特記事項.Text.Trim().ToString();

                ViewState["CurrentTable1"] = dt;
            }
        }
        catch { }
    }
    /// <summary>
    /// ENVENT CLICK INTO GRIDVIEW
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            e.Row.Attributes.Add("onclick", "javascript:__doPostBack('ctl00$ContentPlaceHolder1$grv','LoadUpdate$" + e.Row.RowIndex + "')");
            int ID = Int32.Parse(e.Row.Cells[3].Text);
            if (ID == 0 || ID == 1)
            {
                e.Row.CssClass = "insert";
                //e.Row.BackColor = System.Drawing.Color.LightPink;
                //e.Row.ForeColor = System.Drawing.Color.Maroon;
            }
            if (ID == 2)
            {
                e.Row.CssClass = "update";
                //e.Row.BackColor = System.Drawing.Color.Chocolate;
                //e.Row.ForeColor = System.Drawing.Color.Maroon;
            }
        }
    }
    /// <summary>
    /// EVENT'S BUTTON 登録
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        btn_登録完了.Enabled = true;
        AddNewRow();
        DataCurrentTable();
        ResetForm();
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "aReady();", true);
        this.Title = "入学情報（賞罰）登録";
    }
    /// <summary>
    /// RESET FORM
    /// </summary>
    public void ResetForm()
    {
        Txt_賞罰名.Text = "";
        txt_賞罰年月日.Text = "";
        txt賞罰特記事項.Text = "";
    }
    public void DataColumnView()
    {
        try
        {
            DataTable dtv = new DataTable();
            DataColumn col;

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "賞罰名";
            dtv.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.DateTime");
            col.ColumnName = "賞罰年月日";
            dtv.Columns.Add(col);


            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "賞罰特記事項";
            dtv.Columns.Add(col);

            ViewState["CurrentTable1"] = dtv;
        }
        catch { }
    }
    /// <summary>
    /// ENVENT CLICK INTO GRIDVIEW
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ItemView_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("LoadUpdate"))
            {
                string id = grv.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
                int ID = Int32.Parse(id);
                Session["temp"] = ID.ToString();
                System.Collections.Generic.List<String> myAL = new List<string>();
                if (Session["Array"] != null)
                {
                    myAL = (System.Collections.Generic.List<String>)Session["Array"];
                }
                myAL.Add(ID.ToString());
                Session["Array"] = myAL;
                DataTable dt = (DataTable)ViewState["CurrentTable"];
                foreach (DataRow datarow1 in dt.Rows)
                {
                    int IDtam = Int32.Parse(datarow1["ID"].ToString());
                    if (ID == IDtam)
                    {
                        Txt_賞罰名.Text = datarow1["賞罰名"].ToString();
                        string date = datarow1["賞罰年月日"].ToString();
                        DateTime time = DateTime.Parse(date);
                        date = time.ToString("yyyy/MM/dd");
                        string a = datarow1["賞罰年月日"].ToString();
                        txt_賞罰年月日.Text = date;
                        txt賞罰特記事項.Text = datarow1["賞罰特記事項"].ToString();
                    }
                }
                btn_登録.Visible = false;
                btn_更新.Visible = true;

                Session.Add("登録完了Status", true);

            }
        }
        catch { }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "aReady();", true);
        this.Title = "入学情報（賞罰）登録";
    }
    /// <summary>
    /// EVENT'S BUTTON 更新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_更新_Click(object sender, EventArgs e)
    {
        try
        {
            btn_登録.Visible = true;
            btn_更新.Visible = false;


            string id = Session["temp"].ToString();
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            string a = txt_賞罰年月日.Text.ToString();
            foreach (DataRow row1 in dtCurrentTable.Rows)
            {

                int IDtam = Int32.Parse(row1["ID"].ToString());
                if (Int32.Parse(id) == IDtam)
                {
                    row1["賞罰名"] = Txt_賞罰名.Text.Trim().ToString();
                    row1["賞罰年月日"] = DateTime.Parse(txt_賞罰年月日.Text.ToString());
                    row1["賞罰特記事項"] = txt賞罰特記事項.Text.Trim().ToString();

                    if (row1["IsInit"].ToString() == "3")
                    {
                        row1["IsInit"] = "2";
                    }
                    if (row1["IsInit"].ToString() == "0")
                    {
                        row1["IsInit"] = "1";
                    }

                }

            }
            DataView dv = new DataView(dtCurrentTable);
            ViewState["CurrentTable"] = dtCurrentTable;
            dv.Sort = "IsInit";
            ViewState["CurrentTable"] = dv.ToTable();
            grv.DataSource = dv;
            grv.DataBind();
            btn_登録完了.Enabled = true;
            ResetForm();
            Session.Add("currentId", null);
        }
        catch { }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "aReady();", true);
        this.Title = "入学情報（賞罰）登録";
    }
    /// <summary>
    /// GET ERROR MESSAGE
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid) // ham dung de lay ma loi hien thi thong tin cua loi do.
    {
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        return dt.Rows[0][2].ToString();
    }
    /// <summary>
    /// PAGING
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)// phan trang.
    {
        grv.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        DataView dv = new DataView(dt);
        dv.Sort = "ID DESC";
        grv.DataSource = dv;
        grv.DataSource = dt;
        grv.DataBind();
    }
    /// <summary>
    /// EVENT'S BUTTOM リセット
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        ResetForm();
        btn_登録.Visible = true;
        btn_更新.Visible = false;
        btn_登録完了.Enabled = false;
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "aReady();", true);
        //Response.Redirect("frmJ_TraineeRewPunInput.aspx");
        this.Title = "入学情報（賞罰）登録";
    }


}
